let mongoose = require('mongoose');

let schema = new mongoose.Schema({
    bargains:{
        type:[require('./gameUser.shop.bargain')],
        default:[]
    },
    videoRecords:{
        type:[require('./gameUser.shop.videoRecord')],
        default:[]
    }
});

let config=[
    {type: 101, cost: 100 ,basicCount:1,group:0}, //复活针
    {type: 301, cost: 5000 ,basicCount:1,group:1}, //男皮肤
    {type: 401, cost: 5000 ,basicCount:1,group:1},  //女皮肤
    {type: 302, cost: 6000 ,basicCount:1,group:1}, //新年男皮肤
    {type: 402, cost: 6000 ,basicCount:1,group:1}, //新年女皮肤
    {type: 303, cost: 9999 ,basicCount:1,group:1}, //猪猪男
    {type: 403, cost: 9999 ,basicCount:1,group:1}, //猪猪女
    {type: 304, cost: 8888 ,basicCount:1,group:1}, //可爱男
    {type: 404, cost: 8888 ,basicCount:1,group:1}, //可爱女
    {type: 305, cost: 6000 ,basicCount:1,group:1}, //胖萝卜
    {type: 405, cost: 6000 ,basicCount:1,group:1}, //门牙兔
    {type: 501, cost: 2000 ,basicCount:1,group:2}, //彩色泡泡1
    {type: 502, cost: 2000 ,basicCount:1,group:2}, //彩色泡泡2
    {type: 503, cost: 2000 ,basicCount:1,group:2}, //彩色泡泡3
    {type: 504, cost: 2000 ,basicCount:1,group:2}, //彩色泡泡4
    {type: 201, cost: 50 ,basicCount:1,group:0}, //喇叭
    {type: 300, cost: 1000 ,basicCount:1,group:1}, //男皮肤
    {type: 400, cost: 1000 ,basicCount:1,group:1},  //女皮肤
];

let todayMaxVideoCount=6;

schema.methods={
    getGoods(options) {
        let result=[];
        let skins=options.self.ppt.getSkins(options);
        for(let i=0;i<config.length;i++){
            if(config[i].group===1&&skins.indexOf(config[i].type)>=0){ //已经购买的皮肤直接不显示
                continue;
            }
            if(!config[i].isVideo){
                result.push(config[i]);
            }else{ //处理视频商品需要附加的信息
                let c={};
                let today=new Date().toLocaleDateString();
                let todayVideoRecords=this.videoRecords.filter(item=>{return item.timeDate==today});
                let videoRecords=this.videoRecords.filter(item=>{return item.goodsType==config[i].type;})
                Object.assign(c,config[i],{count:videoRecords.length,todayMaxCount:todayMaxVideoCount,todayCount:todayVideoRecords.length});
                result.push(c);
            }
        }
        return result;
    },
    //视频获取一次
    async videoBuy(options){
        if(!!options.type){
            options.type=parseInt(options.type);
        }
        let allGoods=this.getGoods(options);
        let goods=allGoods.find(item=>{return item.type==options.type});
        if(!goods){
            throw new Error('没有这个商品！');
        }
        if(!goods.isVideo){
            throw new Error('这个商品不可以通过观看视频获取！');
        }
        let today=new Date().toLocaleDateString();
        //let todayVideoRecords=this.videoRecords.filter(item=>{return item.timeDate==today});
        // if(todayVideoRecords.length>=todayMaxVideoCount){
        //     throw new Error('今天已经没有看视频的机会了，明天就可以免费领取了！');
        // }
        this.videoRecords.push({goodsType:options.type,timeDate:today});
        let videoRecords=this.videoRecords.filter(item=>{return item.goodsType==options.type;})
        if(videoRecords.length===goods.cost){
            options.count=1;
            let result=await this.buy(options);
            this.videoRecords=this.videoRecords.filter(item=>{return item.goodsType!=options.type;});
            return result;
        }
    },
    //获得这个商品帮主砍过价的用户名单
    async getBargainUsers(options={type:301}){
        let bargain=this.getBargain(options.type);
        let result=[];
        let GameUserModel=mongoose.models['GameUser'];
        for(let i=0;i<bargain.userIds.length;i++){
            let gameUser=await GameUserModel.findOne({_id:bargain.userIds[i]});
            if(!!gameUser){
                result.push({nickname:gameUser.nickname,headImgUrl:gameUser.headImgUrl});
            }
        }
        return result;
    },
    //帮忙砍价
    async bargain(options={parentId:'',type:1}){
        let GameUserModel=mongoose.models['GameUser'];
        let parentUser=await GameUserModel.findOne({_id:options.parentId});
        if(!!parentUser){
            let id=options.self._id.toString();
            let bargain=parentUser.shop.getBargain(options.type);
            if(bargain.userIds.indexOf(id)>=0){
                throw new Error('您已经帮TA砍过价了。谢谢！');
            }
            if(bargain.userIds.length>=8){
                throw new Error('最多只能邀请8个好友帮忙砍价！');
            }
            bargain.userIds=[].concat(bargain.userIds,[id]);
            await parentUser.save();
        }
    },
    //获得一个商品的砍价记录
    getBargain(type){
        let bargain=this.bargains.find(item=>{return item.type==type;});
        if(!bargain){
            bargain={type:type,userIds:[]};
            this.bargains.push(bargain);
        }
        return bargain;
    },
    buy(options){
        if(!!options.type){
            options.type=parseInt(options.type);
        }
        if(!!options.count){
            options.count=parseInt(options.count);
        }
        let allGoods=this.getGoods(options);
        let goods=allGoods.find(item=>{return item.type==options.type});
        if(!goods){
            throw new Error('没有这个商品！');
        }
        // if(options.belong == 1){
        //     if(options.self.bag.getCount(options.type)){
        //         options.self.ppt.role = options.type;
        //         return
        //     }
        // }

        if(options.count<=0){
            throw new Error('购买数量必须为正整数！');
        }
        //如果是砍价商品
        if(!!goods.isBargain){
            let bargain=this.getBargain(goods.type);
            options.self.store.goldCoin-=Math.floor(goods.cost*(1-0.1*bargain.userIds.length)*options.count);
            bargain.usersIds=[];
            if(options.self.store.goldCoin<0){
                throw new Error('金币不足！');
            }
        }else if(!!goods.isVideo){ //如果是视频商品，自动在看完视频后购买，则此处为空

        }else if(!!goods.isDiamond){
            options.self.store.diamond-=Math.floor(goods.cost*options.count);
            if(options.self.store.diamond<0){
                throw new Error('钻石不足！');
            }
        }else{
            options.self.store.goldCoin-=Math.floor(goods.cost*options.count);
            if(options.self.store.goldCoin<0){
                throw new Error('金币不足！');
            }
        }

        if(goods.group===0){
            //如果是道具，直接到背包
            options.self.bag.addItem(goods.type,options.count);
        }

        if(goods.group===1){
            //新增皮肤
            options.self.ppt.addSkin(goods.type);
            //自动设置该用户启用这个皮肤
            options.self.ppt.useSkin(goods.type);
        }

        if(goods.group===2){
            let type=goods.type%500;
            if(options.self.ppt.bombColors.indexOf(type)<0){
                let arr=[].concat(options.self.ppt.bombColors);
                arr.push(type);
                options.self.ppt.bombColors=arr;
            }else{
                throw new Error('你已经拥有这个泡泡皮肤！');
            }
            options.self.ppt.bombColor=type;
        }

        // if(options.belong == 0) {
        //     options.self.bag.addItem(goods.type,options.count);
        // } else {
        //     options.self.ppt.skin.push({
        //         id:options.type,
        //         vaildTime:-1
        //     })
        //     options.self.ppt.role = options.type
        // }

        return {type:options.type,count:options.count}
    },
};

module.exports=schema;